@PACKAGE_INIT@

include(CMakeFindDependencyMacro)

# export Harfang variables
set(HG_VERSION "@HG_VERSION@")
set(HG_GRAPHIC_API "@HG_GRAPHIC_API@")
set(HG_HOST_PREFIX "@HG_HOST_PREFIX@")
set(HG_TARGET_PREFIX "@HG_TARGET_PREFIX@")
set(HG_USE_GLFW @HG_USE_GLFW@)
set(HG_USE_GTK3 @GTK3_FOUND@)
set(HG_HAS_GLFW @HG_REBUILD_GLFW@)
set(HG_GLFW_BACKEND "@HG_GLFW_BACKEND@")
set(HG_ENABLE_BULLET3_SCENE_PHYSICS @HG_ENABLE_BULLET3_SCENE_PHYSICS@)
set(HG_ENABLE_OPENVR_API @HG_ENABLE_OPENVR_API@)
set(HG_ENABLE_OPENXR_API @HG_ENABLE_OPENXR_API@)
set(HG_ENABLE_SRANIPAL_API @HG_ENABLE_SRANIPAL_API@)
set(HG_ENABLE_RECAST_DETOUR_API @HG_ENABLE_RECAST_DETOUR_API@)

if(HG_ENABLE_OPENVR_API)
	add_compile_definitions(HG_ENABLE_OPENVR_API)
endif()

if(HG_ENABLE_OPENVR_API)
	add_compile_definitions(HG_ENABLE_OPENXR_API)
endif()

if(HG_ENABLE_BULLET3_SCENE_PHYSICS)
	add_compile_definitions(HG_ENABLE_BULLET3_SCENE_PHYSICS)
endif()

if(HG_ENABLE_SRANIPAL_API)
	add_compile_definitions(HG_ENABLE_SRANIPAL_API)
endif()

if(HG_ENABLE_RECAST_DETOUR_API)
	add_compile_definitions(HG_ENABLE_RECAST_DETOUR_API)
endif()

if(HG_USE_GLFW)
	add_compile_definitions(HG_USE_GLFW)
endif()

add_compile_definitions(HG_GRAPHIC_API="${HG_GRAPHIC_API}")
if(HG_GRAPHIC_API STREQUAL "GL")
	set(HG_DEFAULT_RENDERER_TYPE bgfx::RendererType::OpenGL)
elseif(HG_GRAPHIC_API STREQUAL "GLES")
	set(HG_DEFAULT_RENDERER_TYPE bgfx::RendererType::OpenGLES)
elseif(HG_GRAPHIC_API STREQUAL "VK")
	set(HG_DEFAULT_RENDERER_TYPE bgfx::RendererType::Vulkan)
elseif(HG_GRAPHIC_API STREQUAL "DX9")
	set(HG_DEFAULT_RENDERER_TYPE bgfx::RendererType::Direct3D9)
elseif(HG_GRAPHIC_API STREQUAL "DX11")
	set(HG_DEFAULT_RENDERER_TYPE bgfx::RendererType::Direct3D11)
elseif(HG_GRAPHIC_API STREQUAL "DX12")
	set(HG_DEFAULT_RENDERER_TYPE bgfx::RendererType::Direct3D12)
elseif(HG_GRAPHIC_API STREQUAL "MTL")
	set(HG_DEFAULT_RENDERER_TYPE bgfx::RendererType::Metal)
else()
	message(FATAL_ERROR "Unknown graphic API : ${HG_GRAPHIC_API}")
endif()
add_compile_definitions(HG_DEFAULT_RENDERER_TYPE=${HG_DEFAULT_RENDERER_TYPE})

# Unix/Linux specific dependencies
if(UNIX)
	if(NOT HG_HAS_GLFW)
		find_dependency(glfw3 3.2)
		target_compile_definitions(glfw INTERFACE GLFW_@HG_GLFW_BACKEND@=1)
	endif()

	if(NOT APPLE AND NOT ANDROID AND NOT EMSCRIPTEN)
		find_dependency(Threads REQUIRED)

		find_package(PulseAudio)
		find_package(ALSA)
		find_library(JACK_LIBRARY jack)
		find_library(SNDIO_LIBRARY sndio)

		if( HG_GLFW_BACKEND STREQUAL "X11" )
			find_dependency( X11 )
		endif()

		if( (NOT X11_FOUND) OR (HG_GLFW_BACKEND STREQUAL "WAYLAND") )
			find_dependency(ECM REQUIRED NO_MODULE)
			list(APPEND CMAKE_MODULE_PATH "${ECM_MODULE_PATH}")
			find_dependency(Wayland REQUIRED)
		endif()

		find_package(OpenGL)
		if(NOT OPENGL_FOUND)
			find_package(OpenGLES3)
			if(NOT OPENGLES3_FOUND)
				find_package(OpenGLES2 REQUIRED)
			endif()
		endif()

		if(HG_USE_GTK3)
			find_dependency(PkgConfig REQUIRED)
			pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
		endif()

	endif()
endif()

# Import CPPSDK targets
include("${CMAKE_CURRENT_LIST_DIR}/cppsdkTargets.cmake")

# Recreate OpenVR target if needed
if(HG_ENABLE_OPENVR_API)
	get_filename_component(_OPENVR_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" DIRECTORY )
	get_filename_component(_OPENVR_IMPORT_PREFIX "${_OPENVR_IMPORT_PREFIX}" DIRECTORY)

	find_library(OPENVR_LIBRARY_RELEASE name openvr_api PATHS "${_OPENVR_IMPORT_PREFIX}/lib/Release" NO_DEFAULT_PATH)
	find_library(OPENVR_LIBRARY_DEBUG name openvr_api PATHS "${_OPENVR_IMPORT_PREFIX}/lib/Debug" NO_DEFAULT_PATH)

	if(NOT (OPENVR_LIBRARY_RELEASE OR OPENVR_LIBRARY_DEBUG))
		message(FATAL_ERROR "OpenVR library not found")
	endif()

	add_library(OpenVR SHARED IMPORTED GLOBAL)
	set(_OPENVR_IMPORTED_CONFIGS "")
	if(OPENVR_LIBRARY_RELEASE)
		list(APPEND _OPENVR_IMPORTED_CONFIGS RELEASE)
		if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
			find_file(OPENVR_DLL_RELEASE name openvr_api${CMAKE_SHARED_LIBRARY_SUFFIX} PATHS "${_OPENVR_IMPORT_PREFIX}/bin/Release" NO_DEFAULT_PATH)
			set_target_properties(OpenVR PROPERTIES
				IMPORTED_IMPLIB_RELEASE "${OPENVR_LIBRARY_RELEASE}"
				IMPORTED_LOCATION_RELEASE "${OPENVR_DLL_RELEASE}"
			)
		else()
			set_target_properties(OpenVR PROPERTIES
				IMPORTED_LOCATION_RELEASE "${OPENVR_LIBRARY_RELEASE}"
			)
		endif()
	endif()
	if(OPENVR_LIBRARY_DEBUG)
		list(APPEND _OPENVR_IMPORTED_CONFIGS DEBUG)
		if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
			find_file(OPENVR_DLL_DEBUG name openvr_api${CMAKE_SHARED_LIBRARY_SUFFIX} PATHS "${_OPENVR_IMPORT_PREFIX}/bin/Debug" NO_DEFAULT_PATH)
			set_target_properties(OpenVR PROPERTIES
				IMPORTED_IMPLIB_DEBUG "${OPENVR_LIBRARY_DEBUG}"
				IMPORTED_LOCATION_DEBUG "${OPENVR_DLL_DEBUG}"
			)
		else()
			set_target_properties(OpenVR PROPERTIES
				IMPORTED_LOCATION_DEBUG "${OPENVR_LIBRARY_DEBUG}"
			)
		endif()
	endif()
	set_property(TARGET OpenVR PROPERTY IMPORTED_CONFIGURATIONS ${_OPENVR_IMPORTED_CONFIGS})

	unset(_OPENVR_IMPORTED_CONFIGS)
	unset(_OPENVR_IMPORT_PREFIX)
endif()

if(HG_ENABLE_OPENXR_API)
	find_package(Threads REQUIRED)
endif()

# Recreate SRanipal target if needed
if(HG_ENABLE_SRANIPAL_API AND (CMAKE_SYSTEM_NAME STREQUAL "Windows"))
	get_filename_component(_SRANIPAL_IMPORT_PREFIX "${CMAKE_CURRENT_LIST_FILE}" DIRECTORY )
	get_filename_component(_SRANIPAL_IMPORT_PREFIX "${_SRANIPAL_IMPORT_PREFIX}" DIRECTORY)

	find_library(SRANIPAL_LIBRARY_RELEASE name SRanipal PATHS "${_SRANIPAL_IMPORT_PREFIX}/lib/Release" NO_DEFAULT_PATH)
	find_library(SRANIPAL_LIBRARY_DEBUG name SRanipal PATHS "${_SRANIPAL_IMPORT_PREFIX}/lib/Debug" NO_DEFAULT_PATH)
		
	if(NOT (SRANIPAL_LIBRARY_RELEASE OR SRANIPAL_LIBRARY_DEBUG))
		message(FATAL_ERROR "SRanipal library not found")
	endif()

	add_library(SRanipal SHARED IMPORTED GLOBAL)
	set(_SRANIPAL_IMPORTED_CONFIGS "")
	if(SRANIPAL_LIBRARY_RELEASE)
		list(APPEND _SRANIPAL_IMPORTED_CONFIGS RELEASE)
		set(_SRANIPAL_DLLS "")
		foreach(_dll libHTC_License nanomsg SRanipal SRWorks_Log ViveSR_Client)
			find_file(${_dll}_RELEASE name "${_dll}${CMAKE_SHARED_LIBRARY_SUFFIX}" PATHS "${_SRANIPAL_IMPORT_PREFIX}/bin/Release" NO_DEFAULT_PATH)
			list(APPEND _SRANIPAL_DLLS ${${_dll}_RELEASE})
		endforeach(_dll)

		set_target_properties(SRanipal PROPERTIES
			IMPORTED_IMPLIB_RELEASE "${SRANIPAL_LIBRARY_RELEASE}"
			IMPORTED_LOCATION_RELEASE "${_SRANIPAL_DLLS}"
		)
		unset(_SRANIPAL_DLLS)
	endif()

	if(SRANIPAL_LIBRARY_DEBUG)
		list(APPEND _SRANIPAL_IMPORTED_CONFIGS DEBUG)
		set(_SRANIPAL_DLLS "")
		foreach(_dll libHTC_License nanomsg SRanipal SRWorks_Log ViveSR_Client)
			find_file(${_dll}_DEBUG name "${_dll}${CMAKE_SHARED_LIBRARY_SUFFIX}" PATHS "${_SRANIPAL_IMPORT_PREFIX}/bin/Debug" NO_DEFAULT_PATH)		
			list(APPEND _SRANIPAL_DLLS ${${_dll}_DEBUG})
		endforeach(_dll)

		set_target_properties(SRanipal PROPERTIES
			IMPORTED_IMPLIB_DEBUG "${SRANIPAL_LIBRARY_DEBUG}"
			IMPORTED_LOCATION_DEBUG "${_SRANIPAL_DLLS}"
		)
		unset(_SRANIPAL_DLLS)
	endif()
	set_property(TARGET SRanipal PROPERTY IMPORTED_CONFIGURATIONS ${_SRANIPAL_IMPORTED_CONFIGS})

	unset(_SRANIPAL_IMPORTED_CONFIGS)
	unset(_SRANIPAL_IMPORT_PREFIX)
endif()

check_required_components(cppsdk)

# Helper function to deploy shared libraries of CPPSDK external dependencies.
function(install_cppsdk_dependencies destination component)
	foreach(_target @HG_SHARED_LIBRARY_TARGETS@)
		if(TARGET ${_target})
			get_target_property(_target_type ${_target} TYPE)
			if (_target_type STREQUAL "SHARED_LIBRARY")
				get_target_property(_target_location_release ${_target} IMPORTED_LOCATION_RELEASE)
				if(_target_location_release)
					install(FILES ${_target_location_release} DESTINATION ${destination} CONFIGURATIONS Release COMPONENT ${component})
				endif()

				get_target_property(_target_location_debug ${_target} IMPORTED_LOCATION_DEBUG)
				if(_target_location_debug)
					install(FILES ${_target_location_debug} DESTINATION ${destination} CONFIGURATIONS Debug COMPONENT ${component})
				endif()
			endif()
		endif()
	endforeach()
endfunction()
